package geppetto.reordering.graph;

import java.util.ArrayList;
import java.util.HashMap;

import geppetto.phraseHMM.WordTrie;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePairTable;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.SentenceInfo;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.data.ReorderingGraphData;

public class PhraseDependencyTree {

	protected SentenceInfo info;
	protected PhraseDependencyTreeNode[][][][] treeNodes;
	protected HashMap<ExtractedPhrasePair, ReorderingGraphData> ReorderingDataMappings = new HashMap<ExtractedPhrasePair, ReorderingGraphData>();
	
	public PhraseDependencyTree(SentenceInfo info) {
		super();
		this.info = info;
	}
	
	public static ReorderingGraphData generateGraphDataForPhrase(ExtractedPhrasePair epp, WordTrie sourceWordTrie, WordTrie targetWordTrie, ExtractedPhrasePairTable extractionTable) {
		int sourceStartPos = epp.getSourceStartPos();
		int sourceEndPos = epp.getSourceEndPos();
		int targetStartPos = epp.getForeignStartPos();
		int targetEndPos = epp.getForeignEndPos();
		ArrayList<ExtractedPhrasePair> phrases = new ArrayList<ExtractedPhrasePair>();
		ExtractedPhrasePair[][][][] sourceStartArray = extractionTable.getSourceTargetIndexPairs();
		for (int sourceI = sourceStartPos; sourceI <= sourceEndPos; sourceI++){
			ExtractedPhrasePair[][][] sourceEndArray = sourceStartArray[sourceI];
			for (int sourceJ = sourceEndPos; sourceJ >= sourceI; sourceJ--){
				ExtractedPhrasePair[][] targetStartArray = sourceEndArray[sourceJ];
				for(int targetI = targetStartPos; targetI <= targetEndPos; targetI++){
					ExtractedPhrasePair[] targetEndArray = targetStartArray[targetI];
					for(int targetJ = targetEndPos; targetJ >= targetI; targetJ--){
						ExtractedPhrasePair subEpp = targetEndArray[targetJ];
						if(subEpp!=null){
							phrases.add(subEpp);
							subEpp.set_sourceId(sourceWordTrie.getPhraseId(subEpp.getSourcePhrase())); //should put this somewhere else in the code
							subEpp.set_targetId(targetWordTrie.getPhraseId(subEpp.getTargetPhrase()));
						}
					}
				}
			}
		}
		return new ReorderingGraphData(phrases);
	}
}
